
package kaizone.haining.chaobohui.widget;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.WeatherDO.WeatherInfo;
import kaizone.haining.chaobohui.util.Utils;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherView extends LinearLayout {

    public static final int CELL_COUNT = 3;

    public WeatherView(Context context) {
        super(context);
        init();
    }

    public WeatherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        setOrientation(VERTICAL);
    }

    public void setWeatherInfo(WeatherInfo info) {
        if (info == null)
            return;
        for (int i = 0; i < CELL_COUNT; i++) {
            TextView cell = new TextView(getContext());
            String date = Utils.dateAfterHZ(info.date_y, i);
            String week = Utils.getWeekOfStringHZ(date);
            String weather = info.weather[i];
            SpannableString weatcherSpan = weatherSpannable(getContext(), weather);
            String img = info.img[i];
            String temp = info.temp[i];
            String wind = info.wind[i];

            try {
                cell.append(date);
                cell.append("\t");
                cell.append(week);
                cell.append("\t");
                cell.append(weatcherSpan);
                cell.append("\n");
                cell.append(weather);
                cell.append("\t");
                cell.append(temp);
                cell.append("\t");
                cell.append(wind);
                cell.append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }

            addView(cell);
        }
    }

    public final static String HZ_CLOUDY = "多云";

    public final static String HZ_HAIL = "冰雹";

    public final static String HZ_RANNY = "雨";

    public final static String HZ_SNOWY = "雪";

    public final static String HZ_STORM = "暴雨";

    public final static String HZ_SUNNY = "晴";

    public static SpannableString weatherSpannable(Context context, String weather) {
        SpannableString spannableString = new SpannableString("天气");
        ImageSpan imageSpan = null;
        if (weather.equals(HZ_SUNNY)) {
            imageSpan = Utils.imageSpan(context, R.drawable.weather_sunny);
        }//
        else if (weather.contains(HZ_CLOUDY)) {
            if (weather.contains(HZ_SUNNY)) {
                imageSpan = Utils.imageSpan(context, R.drawable.weather_sunnytocloudy);
            } else {
                imageSpan = Utils.imageSpan(context, R.drawable.weather_cloudy);
            }
        }//
        else if (weather.contains(HZ_RANNY)) {
            if (weather.contains(HZ_STORM)) {
                imageSpan = Utils.imageSpan(context, R.drawable.weather_storm);
            } else {
                imageSpan = Utils.imageSpan(context, R.drawable.weather_ranny);
            }
        }//
        else if (weather.contains(HZ_HAIL)) {
            imageSpan = Utils.imageSpan(context, R.drawable.weather_hail);
        }//
        else if (weather.contains(HZ_SNOWY)) {
            imageSpan = Utils.imageSpan(context, R.drawable.weather_snowy);
        }

        spannableString.setSpan(imageSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
}
